<?php
session_start();

// 初始化游戏数据
if (!isset($_SESSION['game'])) {
    $_SESSION['game'] = [
        'characters' => [
            '安智' => [
                'HP' => 100,
                'MP' => 50,
                'EXP' => 0,
                'skills' => ['上网', '网聊'],
                'items' => [],
                'companions' => [],
                'life_skills' => []
            ]
        ],
        'current_character' => '安智',
        'round' => 1,
        'economy_status' => 'normal', // 经济状态：normal, recession, recovery, boom
        'recession_start_round' => null
    ];
}

// 技能列表及效果
$skills = [
    '佛光普照' => function (&$game) {
        foreach ($game['characters'] as &$character) {
            $character['HP'] = 0;
        }
        return "佛光普照，有效范围内全体即死！";
    },
    '时光穿梭' => function (&$game) {
        // 这里简单模拟，实际需要记录前一回合状态
        return "时光穿梭，恢复到前一回合的战斗状态！（简单模拟，未实现完整功能）";
    },
    '星光灭绝' => function (&$game) {
        return "星光灭绝，战斗背景黑屏！";
    },
    '玉如意' => function (&$game) {
        return "玉如意，替换为任意己方技能！（简单模拟，未实现完整功能）";
    },
    '空气净化' => function (&$game) {
        $current_character = $game['current_character'];
        // 简单模拟去除异常状态
        return "空气净化，去除有效范围内己方所有异常状态！（简单模拟，未实现完整功能）";
    },
    '睡眠风' => function (&$game) {
        return "睡眠风，敌方小范围角色昏睡！（简单模拟，未实现敌方系统）";
    },
    '去烟雾' => function (&$game) {
        return "去烟雾，解锁洞穴可视范围！";
    },
    '水循环' => function (&$game) {
        $current_character = $game['current_character'];
        $game['characters'][$current_character]['HP'] += 20;
        return "水循环，增加乙方一人 HP！当前 {$current_character} 的 HP 为 {$game['characters'][$current_character]['HP']}";
    },
    '咖啡循环' => function (&$game) {
        $current_character = $game['current_character'];
        $game['characters'][$current_character]['MP'] += 10;
        return "咖啡循环，增加己方一人 MP！当前 {$current_character} 的 MP 为 {$game['characters'][$current_character]['MP']}";
    },
    '上网' => function (&$game) {
        $current_character = $game['current_character'];
        $new_skill = array_rand($skills);
        $game['characters'][$current_character]['skills'][] = $new_skill;
        return "上网，随机习得新技能 {$new_skill}！";
    },
    '网聊' => function (&$game) {
        $current_character = $game['current_character'];
        // 简单模拟增加亲密度
        return "网聊，增加与己方队友亲密度！（简单模拟，未实现队友系统）";
    },
    '购物' => function (&$game) {
        $current_character = $game['current_character'];
        $items = ['时光车', '万能猫', '大气装置', '游戏机', '循环系统', '电脑', '电视', '手机', '平板'];
        $new_item = $items[array_rand($items)];
        $game['characters'][$current_character]['items'][] = $new_item;
        return "购物，随机获得新道具 {$new_item}！";
    },
    '打游戏' => function (&$game) {
        $current_character = $game['current_character'];
        $game['characters'][$current_character]['EXP'] += 10;
        return "打游戏，提升 EXP！当前 {$current_character} 的 EXP 为 {$game['characters'][$current_character]['EXP']}";
    },
    '学习' => function (&$game) {
        $current_character = $game['current_character'];
        // 简单模拟提升技能等级
        return "学习，提升技能等级！（简单模拟，未实现技能等级系统）";
    },
    '听音乐' => function (&$game) {
        $current_character = $game['current_character'];
        $life_skills = ['钓鱼', '烧饭', '游泳', '跳舞', '唱歌', '武术', '开车', '演奏乐器'];
        $new_life_skill = $life_skills[array_rand($life_skills)];
        $game['characters'][$current_character]['life_skills'][] = $new_life_skill;
        return "听音乐，习得生活技能 {$new_life_skill}！";
    },
    '看电视' => function (&$game) {
        $current_character = $game['current_character'];
        // 简单模拟降低疲劳度
        return "看电视，降低疲劳度！（简单模拟，未实现疲劳度系统）";
    },
    '扫码' => function (&$game) {
        $current_character = $game['current_character'];
        // 简单模拟提升耐久度
        return "扫码，提升耐久度！（简单模拟，未实现耐久度系统）";
    },
    '拍照' => function (&$game) {
        $current_character = $game['current_character'];
        // 简单模拟降低耐久度
        return "拍照，降低耐久度！（简单模拟，未实现耐久度系统）";
    },
    '点平板' => function (&$game) {
        $current_character = $game['current_character'];
        $game['characters'][$current_character]['EXP'] += 15;
        // 简单模拟降低疲劳度
        return "点平板，提升 EXP 并降低疲劳度！当前 {$current_character} 的 EXP 为 {$game['characters'][$current_character]['EXP']}";
    }
];

// 处理技能使用
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['use_skill'])) {
    $skill_name = $_POST['skill'];
    $game = &$_SESSION['game'];
    if (isset($game['characters'][$game['current_character']]) && in_array($skill_name, $game['characters'][$game['current_character']]['skills'])) {
        $result = $skills[$skill_name]($game);
        $message = $result;
    } else {
        $message = "你没有该技能，无法使用！";
    }
    $game['round']++;

    // 处理经济周期
    if ($game['economy_status'] === 'recession') {
        if ($game['round'] - $game['recession_start_round'] >= 10 && $game['round'] - $game['recession_start_round'] <= 11) {
            $game['economy_status'] = 'recovery';
        }
    } elseif ($game['economy_status'] === 'recovery') {
        if ($game['round'] - $game['recession_start_round'] >= 12 && $game['round'] - $game['recession_start_round'] <= 112) {
            $game['economy_status'] = 'boom';
        }
    } elseif ($game['economy_status'] === 'boom') {
        if ($game['round'] - $game['recession_start_round'] > 112) {
            $game['economy_status'] = 'recession';
            $game['recession_start_round'] = $game['round'];
        }
    }
}

// 获取当前游戏数据
$game = $_SESSION['game'];
$currentCharacter = $game['current_character'];
$characterData = $game['characters'][$currentCharacter];

?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟人生网页游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
            background-color: #d4edda;
            color: #155724;
        }
    </style>
</head>

<body>
    <h1>模拟人生网页游戏</h1>
    <p>当前角色：<?php echo $currentCharacter; ?></p>
    <p>HP：<?php echo $characterData['HP']; ?></p>
    <p>MP：<?php echo $characterData['MP']; ?></p>
    <p>EXP：<?php echo $characterData['EXP']; ?></p>
    <p>技能：<?php echo implode(', ', $characterData['skills']); ?></p>
    <p>道具：<?php echo implode(', ', $characterData['items']); ?></p>
    <p>随从：<?php echo implode(', ', $characterData['companions']); ?></p>
    <p>生活技能：<?php echo implode(', ', $characterData['life_skills']); ?></p>
    <p>回合数：<?php echo $game['round']; ?></p>
    <p>经济状态：<?php echo $game['economy_status']; ?></p>

    <?php if (isset($message)): ?>
        <div class="message"><?php echo $message; ?></div>
    <?php endif; ?>

    <form method="post">
        <label for="skill">选择技能：</label>
        <select id="skill" name="skill">
            <?php foreach ($characterData['skills'] as $skill): ?>
                <option value="<?php echo $skill; ?>"><?php echo $skill; ?></option>
            <?php endforeach; ?>
        </select>
        <button type="submit" name="use_skill">使用技能</button>
    </form>
</body>

</html>